package com.xiaowei.gym.gymjsp.ware.dao.impl;

import com.xiaowei.gym.gymjsp.ware.dao.ReservationDao;
import com.xiaowei.gym.gymjsp.ware.entity.ReservationEntity;
import com.xiaowei.gym.gymjsp.ware.util.JdbcUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReservationDaoImpl implements ReservationDao {


    @Override
    public List<ReservationEntity> getList(Integer member_id, int currentPageNo, Integer pageSize) {

        List<ReservationEntity> reservationEntities = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select * from gym_reservation ");
        List<Object> list = new ArrayList<>();
        if (member_id != null && member_id > 0){
            sql.append("where member_id = ?");
            list.add(member_id);
        }
        //在mysql数据库中，分页使用 limit startIndex，pageSize ; 总数
        sql.append(" order by reserved_time DESC limit ?,?");
        currentPageNo = (currentPageNo - 1) * pageSize;
        list.add(currentPageNo);
        list.add(pageSize);
        Object[] params = list.toArray();
        System.out.println("sql ----> " + sql.toString());
        try {
            List<ReservationEntity> reservationEntityList = new ArrayList<>();
            reservationEntities = JdbcUtil.executeQuery(sql.toString(), rs -> {
                while (rs.next()) {
                    ReservationEntity reservationEntity = new ReservationEntity();
                    reservationEntity.setId(rs.getInt("id"));
                    reservationEntity.setMember_id(rs.getInt("member_id"));
                    reservationEntity.setReserved_time(rs.getTimestamp("reserved_time"));
                    reservationEntity.setRemarks(rs.getString("remarks"));
                    reservationEntityList.add(reservationEntity);
                }
                return reservationEntityList;
            }, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return reservationEntities;
    }

    @Override
    public int getCount() {
        String sql = "SELECT count(1) FROM gym_reservation";
        int i = 0;
        try {
            i = JdbcUtil.executeQuery(sql, rs -> {
                int counts = 0;
                while (rs.next()) {
                    counts = rs.getInt("count(1)");
                }
                return counts;
            }, null);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public int createReservation(ReservationEntity reservationEntity) {
        String sql = "INSERT INTO gym_reservation(member_id,reserved_time,remarks) values(?,?,?)";
        Object[] params = {reservationEntity.getMember_id(),reservationEntity.getReserved_time(),reservationEntity.getRemarks()};
        int i = 0;
        try {
             i = JdbcUtil.executeUpdate(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public ReservationEntity getById(Integer valueOf) {
        String sql = "SELECT * FROM gym_reservation WHERE id = ?";
        ReservationEntity reservationEntity = null;
         try {
            reservationEntity = JdbcUtil.executeQuery(sql, rs -> {
                ReservationEntity reservationEntity1 = new ReservationEntity();
                if (rs.next()) {
                    reservationEntity1.setId(rs.getInt("id"));
                    reservationEntity1.setMember_id(rs.getInt("member_id"));
                    reservationEntity1.setReserved_time(rs.getTimestamp("reserved_time"));
                    reservationEntity1.setRemarks(rs.getString("remarks"));
                }
                return reservationEntity1;
            }, valueOf);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return reservationEntity;
    }

    @Override
    public int modify(ReservationEntity reservationEntity) {
        String sql = "UPDATE gym_reservation \n" +
                "SET reserved_time = ?,\n" +
                "remarks = ? \n" +
                "WHERE\n" +
                "\tid = ?";
        Object[] params = {reservationEntity.getReserved_time(),reservationEntity.getRemarks(),reservationEntity.getId()};
        int i = 0;
        try {
            i = JdbcUtil.executeUpdate(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public int[] delByIds(Integer[] delId) {
        String sql = "DELETE FROM gym_reservation WHERE id = ?";
        Object[] params = delId;
        System.out.println(params);
        int[] i = null;
        try {
            i = JdbcUtil.batchDelete(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return i;
    }
}
